package com.sudu.service.impl;

import com.sudu.dao.CommentDao;
import com.sudu.domain.Comment;
import com.sudu.domain.UserInfo;
import com.sudu.service.CommentService;
import com.sudu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao ;

    @Autowired
    private UserService userService ;

    //存放迭代找出的所有子代的集合
    private List<Comment> children = new ArrayList<>();


    private void combineChildren(List<Comment> childComments, String PUsername, int videoId) {
        //判断是否有一级子回复
        if(childComments.size() > 0){
            //循环找出子评论的id
            for(Comment childComment : childComments){
                int uid = childComment.getUserId() ;
                UserInfo userInfo = userService.getUserInfoById(uid) ;
                childComment.setUsername(userInfo.getUsername());
                childComment.setUserImg(userInfo.getHandUrl());
                String parentUsername = userInfo.getUsername() ;
                childComment.setParentNickname(PUsername);
                children.add(childComment);
                int childId = childComment.getComId();
                //查询二级以及所有子集回复
                recursively(childId, parentUsername,videoId);
            }
        }
    }

    private void recursively(int childId, String PUsername , int videoId) {
        //根据子一级评论的id找到子二级评论
        List<Comment> replayComments = commentDao.findByReplayId(childId , videoId );
        if(replayComments.size() > 0){
            for(Comment replayComment : replayComments){
                int uid = replayComment.getUserId() ;
                UserInfo userInfo = userService.getUserInfoById(uid) ;
                replayComment.setUsername(userInfo.getUsername());
                replayComment.setUserImg(userInfo.getHandUrl());
                String parentNickname = userInfo.getUsername();
                replayComment.setParentNickname(PUsername);
                int replayId = replayComment.getComId();
                children.add(replayComment);
                //循环迭代找出子集回复
                recursively(replayId,parentNickname , videoId);
            }
        }
    }

    // 查询评论

    /**
     * 1.根据id为“0”查询出所有父评论
     * 2.根据父评论的id查询出一级子回复
     * 3.根据子回复的id循环迭代查询出所有子集回复
     * 4.将查询出来的子回复放到一个集合中
     * 5.要注意将父评论的姓名给set进去
     * @param videoId
     * @return
     */
    @Override
    public List<Comment> listComment(int videoId) {
    // 查询出父节点
        List<Comment> comments = commentDao.findByParentIdNull(0 , videoId);
        for (Comment comment : comments) {
            int PComId = comment.getComId() ;
            int uid = comment.getUserId() ;
            UserInfo userInfo = userService.getUserInfoById(uid) ;
            comment.setUsername(userInfo.getUsername());
            comment.setUserImg(userInfo.getHandUrl());
            String PUsername = userInfo.getUsername() ;
            List<Comment> childComments = commentDao.findByParentIdNotNull(PComId , videoId);
            //查询出子评论
            combineChildren(childComments, PUsername ,videoId );
            comment.setReplyComments(children);
            children = new ArrayList<>();
        }
        return comments;
    }

    @Override
    public int saveComment(Comment comment ,int uid) {
        comment.setUserId(uid);
        return commentDao.saveComment(comment);
    }

    @Override
    public Boolean deleteFaCom(int cid) {
        return commentDao.deleteFaCom(cid) > 0;
    }

    @Override
    public Boolean deleteChildCom(int cid) {
        return commentDao.deleteChildCom(cid) > 0 ;
    }


}
